package com.onetoall.yjt;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

/**
 * Created by DELL on 2017/1/4.
 */

public class OutAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<String> datas;
    private View view;

    private final int TYPE_FOOTER = 0;
    private final int TYPE_ITEM = 1;
    public static final int TYPE_FOOTER_ADDMORE = 0;
    public static final int TYPE_FOOTER_NOMORE = 1;
    public static final int TYPE_FOOTER_LOADFAIL = 2;
    public static final int TYPE_FOOTER_SUCCESS = 4;
    private int footStatus = 0;

    private final int TYPE_COUNT = 2;

    private boolean showFooter = false;
    public OutAdapter(Context context, List<String> datas){
        this.context = context;
        this.datas= datas;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        if (viewType==TYPE_ITEM){
            view = LayoutInflater.from(context).inflate(R.layout.item_inner,parent,false);
            holder = new MyViewHolder(view);
        }else {
            view = LayoutInflater.from(context).inflate(R.layout.layout_view_footer,parent,false);
            holder = new FootHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//
        if (getItemViewType(position)==TYPE_ITEM){
            ( (MyViewHolder) holder).textView.setText(datas.get(position));
        }else {
            FootHolder footHolder = ((FootHolder)holder);
            switch (footStatus){
                case TYPE_FOOTER_ADDMORE:
                    footHolder.bar.setVisibility(View.VISIBLE);
                    footHolder.text.setText("正在加载");
                    break;
                case TYPE_FOOTER_LOADFAIL:
                    footHolder.bar.setVisibility(View.GONE);
                    footHolder.text.setText("加载失败,请检查网络");
                    break;
                case TYPE_FOOTER_NOMORE:
                    footHolder.bar.setVisibility(View.GONE);
                    footHolder.text.setText("暂无更多");
                    break;
                case TYPE_FOOTER_SUCCESS:
                    footHolder.bar.setVisibility(View.GONE);
                    footHolder.text.setText("加载更多");
                    break;
            }
        }

    }

    @Override
    public int getItemCount() {
        if (showFooter) {
            return datas.size() + 1;
        } else {
            return datas.size();
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position < datas.size()) {
            return TYPE_ITEM;
        } else {
            return TYPE_FOOTER;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
         public TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_inner_content);
        }
     }

    class FootHolder extends RecyclerView.ViewHolder{
        ProgressBar bar;
        TextView text;
        public FootHolder(View itemView) {
            super(itemView);
            bar = (ProgressBar) itemView.findViewById(R.id.footer_pro);
            text = (TextView) itemView.findViewById(R.id.footer_text_msg);
        }
    }
    public void updateFootStatus(int status) {
        this.footStatus = status;
    }

    public int getFootStatus() {
        return footStatus;
    }

    public void setShowFooter(boolean showFooter) {
        this.showFooter = showFooter;
    }

    public boolean isShowFooter() {
        return showFooter;
    }
}
